//Check for broken images in a page

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.junit.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class BrokenLinksOfPage {

	WebDriver myFD = new FirefoxDriver();
	String vURL = "http://www.pennywisesolutions.com";
	@Test
	public void checkforbrokenimages() throws Exception{
		
		myFD.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		myFD.get(vURL);  
		//myFD.findElement(By.name("q")).sendKeys("Test"); 
		
		List<WebElement> LinksList = myFD.findElements(By.tagName("a"));
		int countBrokenLinks = 0;
		for (WebElement link : LinksList)
		{
		    HttpResponse response = new DefaultHttpClient().execute(new HttpGet(link.getAttribute("href")));
		   /* if(!(new HttpGet(link.getAttribute("href")).equals("javascript:")))
		    {*/
		    if (response.getStatusLine().getStatusCode() != 200){
		        // Do whatever you want with broken links
		    	String  currentLinkUrl = link.getAttribute("href");
		    	System.out.println(currentLinkUrl);
		    	countBrokenLinks++;
		    
		}	
		}
		System.out.println("The number of Broken URLs are: " + countBrokenLinks);
}
}